package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import model.CustomInfo;
import model.EventFieldsCondition;
import model.Field;
import dao.CustomInfoDao;
import dao.EventFieldsConditionDao;
import dao.FieldDao;
import utils.RequestUtil;
import utils.ResponseUtil;

/**
 * Servlet implementation class EventFieldsConditionServlet
 */
@WebServlet("/EventFieldsConditionServlet")
public class EventFieldsConditionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       



	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = RequestUtil.get(request.getParameter("method"));
		if (method.equals("getEventFilesConditionByUeid")) {
			getEventFilesConditionByUeid(request, response);
		}else if (method.equals("getFieldListByTid")) {
			getFieldListByTid(request,response);
		}else if(method.equals("addEventFilesCondition")){
			addEventFilesCondition(request,response);
		}
		else if(method.equals("getFieldListByUeid")){
			getFieldListByUeid(request,response);
		}
	}
	
	private void getEventFilesConditionByUeid(HttpServletRequest request, HttpServletResponse response) {
		String ueId = RequestUtil.get(request.getParameter("ueId"));
		if (ueId.equals(""))
			return;
		int id = Integer.parseInt(ueId);
		EventFieldsConditionDao efcDao = new EventFieldsConditionDao();
		List<EventFieldsCondition> efcList = efcDao.getAllByUeId(id);
		request.setAttribute("efcList", efcList);
		try {
			//所要显示的efcListBody.jsp，可以被显示在eventInfoList.jsp中的efcListBody的这个div
			request.getRequestDispatcher("efcListBody.jsp").forward(request, response);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
	}
	
	private void getFieldListByUeid(HttpServletRequest request, HttpServletResponse response) {
		String ueId = RequestUtil.get(request.getParameter("ueId"));
		if (ueId.equals(""))
			return;
	//	int id = Integer.parseInt(ueId);
		CustomInfoDao ciDao = new CustomInfoDao();
		List<CustomInfo> efcList = ciDao.getCustomInfoListByUeid(ueId);
		request.setAttribute("efcList", efcList);
		try {
			//所要显示的efcListBody.jsp，可以被显示在eventInfoList.jsp中的efcListBody的这个div
			request.getRequestDispatcher("efcListBody.jsp").forward(request, response);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
	}
	
	private void getFieldListByTid(HttpServletRequest request,
			HttpServletResponse response) {
		String tId = RequestUtil.get(request.getParameter("tId"));
		if (tId.equals(""))
			return;
		CustomInfoDao ciDao = new CustomInfoDao();
		List<CustomInfo> efcList = ciDao.getCustomInfoListByTid(tId);
		request.setAttribute("efcList", efcList);
		try {
			//所要显示的efcListBody.jsp，可以被显示在eventInfoList.jsp中的efcListBody的这个div
			request.getRequestDispatcher("efcListBody.jsp").forward(request, response);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
	}
	private void addEventFilesCondition(HttpServletRequest request,
			HttpServletResponse response){
		String field = RequestUtil.get(request.getParameter("fields"));
		String condition = RequestUtil.get(request.getParameter("condition"));
		String value= RequestUtil.get(request.getParameter("value"));
		String ueId = RequestUtil.get(request.getParameter("ueId"));
		JSONObject result = new JSONObject();
		EventFieldsCondition efc = new EventFieldsCondition(Integer.parseInt(field), Integer.parseInt(ueId), condition,value);
		EventFieldsConditionDao efcDao = new EventFieldsConditionDao();
		efcDao.save(efc);
		result.put("succeed", "0");
		
		try {
			ResponseUtil.write(result, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
